import pygame
import random
import sys

# 初始化Pygame
pygame.init()

# 设置屏幕大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("飞机大战")

# 加载图像
player_img = pygame.image.load("player.svg").convert_alpha()
enemy_img = pygame.image.load("enemy.svg").convert_alpha()
bullet_img = pygame.image.load("bullte.svg").convert_alpha()
explosion_img = pygame.image.load("explosion.svg").convert_alpha()

# 加载声音
bullet_sound = pygame.mixer.Sound("sound.wav")

# 设置玩家初始位置
player_x = screen_width // 2
player_y = screen_height - 100
player_speed = 5

# 子弹列表
bullets = []
bullet_speed = 10

# 敌人列表
enemies = []
enemy_speed = 3

# 游戏循环标志
running = True

# 游戏时钟
clock = pygame.time.Clock()

# 玩家类
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = player_img
        self.rect = self.image.get_rect(center=(player_x, player_y))

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and self.rect.left > 0:
            self.rect.x -= player_speed
        if keys[pygame.K_RIGHT] and self.rect.right < screen_width:
            self.rect.x += player_speed

    def shoot(self):
        bullet = Bullet(self.rect.centerx, self.rect.top)
        bullets.append(bullet)
        bullet_sound.play()

# 子弹类
class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = bullet_img
        self.rect = self.image.get_rect(center=(x, y))

    def update(self):
        self.rect.y -= bullet_speed
        if self.rect.bottom < 0:
            self.kill()

# 敌人类
class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = enemy_img
        self.rect = self.image.get_rect(center=(random.randint(50, screen_width - 50), -50))

    def update(self):
        self.rect.y += enemy_speed
        if self.rect.top > screen_height:
            self.kill()

# 爆炸类
class Explosion(pygame.sprite.Sprite):
    def __init__(self, center):
        super().__init__()
        self.image = explosion_img
        self.rect = self.image.get_rect(center=center)
        self.frame = 0
        self.last_update = pygame.time.get_ticks()
        self.frame_rate = 50

    def update(self):
        now = pygame.time.get_ticks()
        if now - self.last_update > self.frame_rate:
            self.last_update = now
            self.frame += 1
            if self.frame == len(explosion_img):
                self.kill()
            else:
                center = self.rect.center
                self.image = explosion_img
                self.rect = self.image.get_rect(center=center)

# 创建玩家实例
player = Player()
all_sprites = pygame.sprite.Group(player)
all_enemies = pygame.sprite.Group()

# 游戏主循环
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.shoot()

    # 更新所有精灵
    all_sprites.update()
    for bullet in bullets:
        all_sprites.add(bullet)
    all_enemies.update()

    # 生成敌人
    if random.randint(1, 100) < 5:
        enemy = Enemy()
        all_enemies.add(enemy)
        all_sprites.add(enemy)

    # 检测碰撞
    hits = pygame.sprite.groupcollide(all_enemies, bullets, True, True)
    for hit in hits:
        explosion = Explosion(hit.rect.center)
        all_sprites.add(explosion)

    # 绘制屏幕
    screen.fill((0, 0, 0))
    all_sprites.draw(screen)
    pygame.display.flip()

    # 控制帧率
    clock.tick(60)

# 退出游戏
pygame.quit()
sys.exit()